"""系统日志表(SysLog)的Pydantic模型

包含用于API接口的Base、Create、Update、Public模型
"""

from datetime import datetime
from typing import Optional

from pydantic import Field, field_serializer

from app.schemas.base import BaseSchema


class LogBase(BaseSchema):
    """系统日志表基础模型"""

    module: str = Field(max_length=50, description="日志模块", alias="module")
    request_method: str = Field(max_length=64, description="请求方式", alias="requestMethod")
    content: str = Field(max_length=255, description="日志内容", alias="content")
    request_params: Optional[str] = Field(
        default=None, description="请求参数(批量请求参数可能会超过text)", alias="requestParams"
    )
    response_content: Optional[str] = Field(default=None, description="返回参数", alias="responseContent")
    request_uri: Optional[str] = Field(default=None, max_length=255, description="请求路径", alias="requestUri")
    method: Optional[str] = Field(default=None, max_length=255, description="方法名", alias="method")
    ip: Optional[str] = Field(default=None, max_length=45, description="IP地址", alias="ip")
    province: Optional[str] = Field(default=None, max_length=100, description="省份", alias="province")
    city: Optional[str] = Field(default=None, max_length=100, description="城市", alias="city")
    execution_time: Optional[int] = Field(default=None, description="执行时间(ms)", alias="executionTime")
    browser: Optional[str] = Field(default=None, max_length=100, description="浏览器", alias="browser")
    browser_version: Optional[str] = Field(
        default=None, max_length=100, description="浏览器版本", alias="browserVersion"
    )
    os: Optional[str] = Field(default=None, max_length=100, description="终端系统", alias="os")
    create_by: Optional[int] = Field(default=None, description="创建人ID", alias="createBy")


class LogCreate(LogBase):
    """创建系统日志表时使用的模型"""

    pass


class LogUpdate(LogBase):
    """更新系统日志表时使用的模型"""

    module: Optional[str] = Field(default=None, max_length=50, description="日志模块", alias="module")
    request_method: Optional[str] = Field(default=None, max_length=64, description="请求方式", alias="requestMethod")
    content: Optional[str] = Field(default=None, max_length=255, description="日志内容", alias="content")
    request_params: Optional[str] = Field(
        default=None, description="请求参数(批量请求参数可能会超过text)", alias="requestParams"
    )
    response_content: Optional[str] = Field(default=None, description="返回参数", alias="responseContent")
    request_uri: Optional[str] = Field(default=None, max_length=255, description="请求路径", alias="requestUri")
    method: Optional[str] = Field(default=None, max_length=255, description="方法名", alias="method")
    ip: Optional[str] = Field(default=None, max_length=45, description="IP地址", alias="ip")
    province: Optional[str] = Field(default=None, max_length=100, description="省份", alias="province")
    city: Optional[str] = Field(default=None, max_length=100, description="城市", alias="city")
    execution_time: Optional[int] = Field(default=None, description="执行时间(ms)", alias="executionTime")
    browser: Optional[str] = Field(default=None, max_length=100, description="浏览器", alias="browser")
    browser_version: Optional[str] = Field(
        default=None, max_length=100, description="浏览器版本", alias="browserVersion"
    )
    os: Optional[str] = Field(default=None, max_length=100, description="终端系统", alias="os")
    create_by: Optional[int] = Field(default=None, description="创建人ID", alias="createBy")


class LogPublic(LogBase):
    """系统日志表公开模型，用于API响应"""

    id: int = Field(alias="id", description="主键")
    module: str = Field(alias="module", description="日志模块")
    request_method: str = Field(alias="requestMethod", description="请求方式")
    content: str = Field(alias="content", description="日志内容")
    request_params: Optional[str] = Field(
        default=None, alias="requestParams", description="请求参数(批量请求参数可能会超过text)"
    )
    response_content: Optional[str] = Field(default=None, alias="responseContent", description="返回参数")
    request_uri: Optional[str] = Field(default=None, alias="requestUri", description="请求路径")
    method: Optional[str] = Field(default=None, alias="method", description="方法名")
    ip: Optional[str] = Field(default=None, alias="ip", description="IP地址")
    province: Optional[str] = Field(default=None, alias="province", description="省份")
    city: Optional[str] = Field(default=None, alias="city", description="城市")
    execution_time: Optional[int] = Field(default=None, alias="executionTime", description="执行时间(ms)")
    browser: Optional[str] = Field(default=None, alias="browser", description="浏览器")
    browser_version: Optional[str] = Field(default=None, alias="browserVersion", description="浏览器版本")
    os: Optional[str] = Field(default=None, alias="os", description="终端系统")
    create_by: Optional[int] = Field(default=None, alias="createBy", description="创建人ID")
    create_time: Optional[datetime] = Field(default=None, alias="createTime", description="创建时间")


class LogPageVO(BaseSchema):
    """系统日志分页VO"""

    id: int = Field(alias="id", description="主键")
    module: str = Field(alias="module", description="日志模块")
    content: str = Field(alias="content", description="日志内容")
    request_uri: str = Field(alias="requestUri", description="请求路径")
    method: str = Field(alias="method", description="请求方法")
    ip: str = Field(alias="ip", description="IP 地址")
    region: str = Field(alias="region", description="地区")
    browser: str = Field(alias="browser", description="浏览器")
    os: str = Field(alias="os", description="终端系统")
    execution_time: int = Field(alias="executionTime", description="执行时间(毫秒)")
    create_by: Optional[int] = Field(None, alias="createBy", description="创建人ID")
    create_time: datetime = Field(alias="createTime", description="创建时间")
    operator: Optional[str] = Field(None, alias="operator", description="操作人")

    @field_serializer("create_time", when_used="json")
    def serialize_create_time(self, create_time: datetime, _info) -> str:
        """格式化create_time字段"""
        return create_time.strftime("%Y-%m-%d %H:%M:%S")


class VisitStatsVO(BaseSchema):
    """访问量统计视图对象"""

    today_uv_count: Optional[int] = Field(default=None, alias="todayUvCount", description="今日独立访客数 (UV)")
    total_uv_count: Optional[int] = Field(default=None, alias="totalUvCount", description="累计独立访客数 (UV)")
    uv_growth_rate: Optional[float] = Field(default=None, alias="uvGrowthRate", description="独立访客增长率")
    today_pv_count: Optional[int] = Field(default=None, alias="todayPvCount", description="今日页面浏览量 (PV)")
    total_pv_count: Optional[int] = Field(default=None, alias="totalPvCount", description="累计页面浏览量 (PV)")
    pv_growth_rate: Optional[float] = Field(default=None, alias="pvGrowthRate", description="页面浏览量增长率")


class VisitTrendVO(BaseSchema):
    """访问趋势视图对象"""

    dates: list[str] = Field(default_factory=list, alias="dates", description="日期列表")
    pv_list: list[int] = Field(default_factory=list, alias="pvList", description="浏览量(PV)")
    ip_list: list[int] = Field(default_factory=list, alias="ipList", description="IP数")
